package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.dao.UserOrderInfoDao;
import cn.icanci.ladybird.pojo.dto.UserOrderDTO;
import cn.icanci.ladybird.pojo.entity.OrderInfo;
import cn.icanci.ladybird.pojo.entity.UserInfoOrderInfoMiddle;
import cn.icanci.ladybird.service.OrderInfoService;
import cn.icanci.ladybird.service.UserInfoOrderInfoMiddleService;
import cn.icanci.ladybird.service.user.UserOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: YPF
 * @date: Created in 2020/10/4 14:58
 * @classAction:
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {
    @Autowired
    private UserOrderInfoDao userOrderInfoDao;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private UserInfoOrderInfoMiddleService userInfoOrderInfoMiddleService;
    @Override
    public List<UserOrderDTO> userOrderDTO() {
        ArrayList<UserOrderDTO> userOrderDTOs = new ArrayList<>();
        List<UserInfoOrderInfoMiddle> userInfoOrderInfoMiddleList = userInfoOrderInfoMiddleService.queryByUserInfoId(1L);
        for (UserInfoOrderInfoMiddle userInfoOrderInfoMiddle : userInfoOrderInfoMiddleList) {
            Long orderId = userInfoOrderInfoMiddle.getOrderInfoMiddleId();
            OrderInfo orderInfo = orderInfoService.queryByOrderInfoId(orderId);
            if (null!=orderInfo){
                UserOrderDTO userOrderDTO = new UserOrderDTO();
                userOrderDTO.setUserInfoId(1L);
                userOrderDTO.setOrderInfoId(orderInfo.getOrderInfoId());
                userOrderDTO.setOrderNumber(orderInfo.getOrderNumber());
                userOrderDTO.setCreateTime(orderInfo.getCreateTime());
                userOrderDTO.setOrderInfoStatus(orderInfo.getOrderInfoStatus());
                userOrderDTO.setOrderInfoTotalPrice(orderInfo.getOrderInfoTotalPrice());
                userOrderDTO.setTicketDetailsInfo(orderInfo.getTicketDetailsInfo());
                userOrderDTO.setTicketTime(orderInfo.getTicketTime());
                userOrderDTO.setTicketAddressVenue(orderInfo.getTicketAddressVenue());
                userOrderDTO.setSeats(orderInfo.getSeats());
                String str = orderInfo.getSeats();
                int x = str.split(",").length;
                userOrderDTO.setTicketNum(x);
                userOrderDTO.setSeats(orderInfo.getSeats());
                userOrderDTOs.add(userOrderDTO);
            }
        }
        return userOrderDTOs;
    }

    @Transactional
    @Override
    public OrderInfo updateByOrderInfoId(Long orderInfoId) {
        OrderInfo orderInfo = orderInfoService.queryByOrderInfoId(orderInfoId);
        orderInfo.setOrderInfoStatus(2);
        OrderInfo order =  orderInfoService.update(orderInfo);
        return order;
    }

    @Override
    public PageInfo<UserOrderDTO> queryAllByNotObject(Long userInfoId, Integer pageNum, Integer pageSize) {
        // 分页
        // pageNum：当前页数   pageSize：当前页需要显示的数量
        // 只要有一个没有传值，那么默认就是首页，然后设置为首页
        if (null == pageNum || null == pageSize) {
            pageNum = 1;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        List<UserOrderDTO> userOrderDTOList = this.userOrderInfoDao.queryAllByNotObject(userInfoId);
        // 把查询的结果封装到 PageInfo
        PageInfo<UserOrderDTO> userOrderDTOPageInfo = new PageInfo<>(userOrderDTOList);
        return userOrderDTOPageInfo;
    }
}
